#include <Windows.h>
#include <GdiPlus.h>

#include <iostream>
#include <fstream>

#pragma comment(lib, "GdiPlus.lib")
#pragma comment(lib, "tess.lib")

using namespace Gdiplus;
using namespace std;


extern "C" __declspec(dllimport) bool
	Recognize(
		const char*		lpLanguage,		/*e.g. eng, chs, null means eng*/
		bool			numeric_mode,
		const unsigned char* buffer,	/*image buffer*/
		int				bytes_per_pixel,
		int				bytes_per_line,
		int				left, 
		int				top,
		int				width, 
		int				height,
		char*			lpText,			/*output text buffer*/
		int				nSize			/*maximize size*/
		);

int wmain(int argc, wchar_t **argv)
{
	if(argc != 2)
	{
		cout << "tess-exec image" << endl;
		return -1;
	}

	GdiplusStartupInput gdiplusStartupInput;
	ULONG_PTR           gdiplusToken;

	// Initialize GDI+.
	GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
	Image* img = new Bitmap(argv[1]);
	BitmapData* data = new BitmapData();
	Rect rect(0, 0, img->GetWidth(), img->GetHeight());

	Bitmap* bmp = dynamic_cast<Bitmap*>(img);
	if(bmp)
		bmp->LockBits(&rect, ImageLockModeRead, PixelFormat32bppARGB, data);

	char text[64];
	Recognize(NULL, true, (unsigned char*)data->Scan0, 
		GetPixelFormatSize(data->PixelFormat)/8, data->Stride,
		0, 0, data->Width, data->Height,
		text, 64);
	cout << text << endl;
	
	if(bmp)
		bmp->UnlockBits(data);
	delete data;
	delete img;
	GdiplusShutdown(gdiplusToken);
	return 0;
}

